//
//  FeedBackViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/9/8.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "FeedBackViewController.h"
#import "BaseLabel.h"
#import "Masonry.h"
#import "BaseTextField.h"
#import "BaseTextView.h"
#import "BaseLabel.h"
#import "BaseButton.h"
#import "DLWNetworkingParser.h"
@interface FeedBackViewController ()<UITextFieldDelegate, UITextViewDelegate>
@property (nonatomic, strong) BaseTextField *textField;
@property (nonatomic, strong) BaseTextView *textView;
@property (nonatomic, strong) BaseLabel *lable;
@property (nonatomic, strong) BaseButton *buttonCommit;
@property (nonatomic, strong) UIAlertController *alertController;
@end

@implementation FeedBackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"意见反馈";
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"night" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"day" object:nil];

    self.view.backgroundColor = [UIColor colorWithWhite:0.942 alpha:1.000];
    [self createSubView];
}

- (void)createSubView
{
    BaseLabel *lableHeader = [[BaseLabel alloc] init];
    lableHeader.text = @"您的意见可以帮助我们做得更好";
    [self.view addSubview:lableHeader];
    lableHeader.textColor = [UIColor grayColor];
    [lableHeader mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view.mas_top);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    _textField = [[BaseTextField alloc] init];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.placeholder = @"输入信息标题";
    _textField.delegate = self;
    [self.view addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(lableHeader.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
        make.height.mas_equalTo(50);
    }];
    
    _textView = [[BaseTextView alloc] init];
    _textView.font = [UIFont systemFontOfSize:18];
    _textView.delegate = self;
    [self.view addSubview:_textView];
    [_textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textField.mas_bottom).mas_offset(10);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
        make.height.mas_equalTo(180);
    }];
    
    _lable = [[BaseLabel alloc] init];
    _lable.text = @"非常乐意聆听您对蛋鸡管家的意见或建议";
    [_textView addSubview:_lable];
    _lable.textColor = [UIColor grayColor];
    [_lable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textView.mas_top).mas_offset(5);
        make.left.mas_equalTo(_textView.mas_left).mas_offset(10);
        make.right.mas_equalTo(_textView.mas_right).mas_offset(-5);
        make.height.mas_equalTo(30);
    }];
    
    _buttonCommit = [BaseButton buttonWithType:UIButtonTypeSystem];
    _buttonCommit.titleLabel.font = [UIFont systemFontOfSize:20];
    [_buttonCommit setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_buttonCommit setTitle:@"提交" forState:UIControlStateNormal];
    _buttonCommit.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    [_buttonCommit addTarget:self action:@selector(buttonActionCommit:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonCommit];
    
    [_buttonCommit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textView.mas_bottom).mas_offset(20);
        make.left.mas_equalTo(self.view.mas_left).mas_offset(20);
        make.right.mas_equalTo(self.view.mas_right).mas_offset(-20);
        make.height.mas_equalTo(40);
    }];
    // 创建一个提示框
    _alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [_alertController addAction:actionCancel];
}
//// 这里的textView 编辑开始,带入的,
//- (void)textViewDidBeginEditing:(UITextView *)textView
//{
//    if (textView.text.length > 0) {
//        _lable.hidden = YES;
//    } else
//    {
//        _lable.hidden = NO;
//    }
//}

#if 1
// 这里的textView 是每次改变都会带入
- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 0) {
        _lable.hidden = YES;
    } else
    {
        _lable.hidden = NO;
    }
}

#endif
//#pragma mark - 给textView添加一个观察者
//- (void)textViewAddObserver
//{
//    [_textView addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
//}
//- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
//{
//    NSLog(@"%@", change);
//}
//



- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView
{
    [textView resignFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - button 的点击事件
- (void)buttonActionCommit:(BaseButton *)button
{
    if ([_textView.text isEqualToString:@""]) {
        _alertController.message = @"建议内容不能为空";
        [self presentViewController:_alertController animated:YES completion:nil];
    }
    else if ([_textField.text isEqualToString:@""])
    {
        _alertController.message = @"标题不能为空";
        [self presentViewController:_alertController animated:YES completion:nil];
    }
    else if (![_textField.text isEqualToString:@""] && ![_textView.text isEqualToString:@""])
    {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_textField.text,@"title", _textView.text, @"content", nil];
    [DLWNetworkingParser POSTWithURL:@"http://www.danjiguanjia.com/api/Feedback/Feedback" par:dic success:^(id responseObject) {
        NSString *string = responseObject[@"msg"];
        if ([string isEqualToString:@"提交成功"]) {
            _alertController.message = @"谢谢您提的宝贵意见";
            [self presentViewController:_alertController animated:YES completion:^{
                
            }];
        }
    } filed:^(NSError *error) {
        
    }];
        
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
